﻿// owner 蒋家伟

#include "ExternalDataComponent.h"

// jsdk
#include "ObjectReflectDefine.h"

OBJECT_REFLECT_DEFINE(jsdk, ExternalDataComponent)
{
}

void jsdk::ExternalDataComponent::setOwnerElement(IElement* pOwnerElement)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pOwnerElement, L"传入了无效的所属对象指针", L"蒋家伟", L"2023-12-18");
    for (auto& iter : getkeyToExternalDataUMapFW__())
    {
        DBG_WARN_AND_CONTINUE_UNLESS(iter.second, L"存在无效的外部数据，其键值:" + iter.first, L"蒋家伟", L"2023-12-19");
        iter.second->setOwnerElement(pOwnerElement);
    }
}

jsdk::OwnerPtr<jsdk::ExternalDataComponent> jsdk::ExternalDataComponent::create()
{
    return NEW_AS_OWNERPTR(jsdk::ExternalDataComponent);
}

bool jsdk::ExternalDataComponent::registerExternalData(const std::wstring& key, OwnerPtr<IExternalData> opExternalData)
{
    return getkeyToExternalDataUMapFW__().insert(std::make_pair(key, transformOwnerShip(opExternalData))).second;
}

const jsdk::IExternalData* jsdk::ExternalDataComponent::getExternalData(const std::wstring& externalDataKey) const
{
    const auto& iter = getkeyToExternalDataUMap__().find(externalDataKey);
    if (iter != getkeyToExternalDataUMap__().end())
    {
        return iter->second.get();
    }
    return nullptr;
}

jsdk::IExternalData* jsdk::ExternalDataComponent::getExternalData(const std::wstring& externalDataKey)
{
    const auto& iter = getkeyToExternalDataUMapFW__().find(externalDataKey);
    if (iter != getkeyToExternalDataUMap__().end())
    {
        return iter->second.get();
    }
    return nullptr;
}
